package router

import (
	backend "blog/controller/backend"
	frontend "blog/controller/frontend"

	"github.com/gin-gonic/gin"
)

func InitRouter(r *gin.Engine) {
	// 静态文件服务
	r.Static("/static", "./static")

	// 前台路由组
	frontendGroup := r.Group("/")
	{
		frontendGroup.GET("/", frontend.IndexHandle)
		frontendGroup.GET("/read/:id", frontend.ReadHandle)
		frontendGroup.GET("/search", frontend.SearchHandle)
		frontendGroup.GET("/archive", frontend.ArchiveHandle)
		frontendGroup.GET("/about", frontend.AboutHandle)
		frontendGroup.GET("/addLink", frontend.AddLinkHandle)
		frontendGroup.POST("/addLink", frontend.ToAddLinkHandle)
		frontendGroup.POST("/comment", frontend.CommentHandle)
	}

	// 后台路由组 - 不需要登录验证的部分
	adminPublicGroup := r.Group("/admin")
	{
		adminPublicGroup.GET("/", backend.Login)
		adminPublicGroup.POST("/login", backend.Login)
		adminPublicGroup.GET("/logout", backend.Logout)
	}

	// 后台路由组 - 需要登录验证的部分
	adminAuthGroup := r.Group("/admin")
	adminAuthGroup.Use(authMiddleware())
	{
		adminAuthGroup.GET("/home", backend.Home)
		adminAuthGroup.GET("/search", backend.Search)
		adminAuthGroup.GET("/posts", backend.Posts)
		adminAuthGroup.GET("/posts/info/:id", backend.PostsInfo)
		adminAuthGroup.GET("/suggest", backend.Suggest)
		adminAuthGroup.GET("/admins", backend.Admins)
		adminAuthGroup.GET("/version", backend.Version)
		adminAuthGroup.GET("/backup", backend.Backup)
		adminAuthGroup.GET("/account", backend.Account)
		adminAuthGroup.GET("/about", backend.About)
		adminAuthGroup.GET("/siteOpt", backend.SiteOpt)
		adminAuthGroup.GET("/setting", backend.Setting)
		adminAuthGroup.GET("/link", backend.Link)
		adminAuthGroup.GET("/link/all", backend.LinkAll)
		adminAuthGroup.POST("/link/add", backend.LinkAdd)
		adminAuthGroup.GET("/type", backend.Type)
		adminAuthGroup.GET("/type/all", backend.TypeAll)
		adminAuthGroup.POST("/type/add", backend.TypeAdd)
		adminAuthGroup.POST("/type/update", backend.TypeUpdate)
		adminAuthGroup.GET("/cache", backend.Cache)
		adminAuthGroup.POST("/cache/update", backend.CacheUpdate)
	}
}
